package com.qqtech.qquan.hi.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.agent.context.AdminPage;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.hi.service.HiRuleResultService;
import com.qqtech.qquan.sys.controller.BaseAgentController;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.model.UserValue;
import com.qqtech.qquan.user.model.UserValueQuery;
import com.qqtech.qquan.user.model.UserValueVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserValueService;

@Controller
@RequestMapping(value = "/hi/manual")
public class HiManualController extends BaseAgentController {

	@Resource
	private UserValueService userValueService;
	@Resource
	private UserBaseService userBaseService;
	@Resource
	private HiRuleResultService hiRuleResultService;

	@Override
	protected BaseService<UserValue> getBaseService() {
		return userValueService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/hi/hiManual-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(UserValueQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<UserValueVo> page = userValueService.queryUserInfo(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	/**
	 * 打开【手工奖惩】页面
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/toHandDo")
	public String toHandDo(Integer id, Model model) {
		UserBaseVo userBaseVo = userBaseService.getById(id);
		UserValueVo userValueVo = userValueService.getById(id);
		model.addAttribute("userBaseVo", userBaseVo);
		model.addAttribute("userValueVo", userValueVo);
		return "/hi/hiManual-add";
	}

	@RequestMapping(value = "/submitHand")
	@ResponseBody
	public Result submitHand(UserValueQuery userValue) {
		Result ret = new Result();
		Integer userId = userValue.getId();
		Integer addDeleFlag = userValue.getAddDeleFlag();
		Integer honorValue = userValue.getHonorValue();
		Integer quanValue = userValue.getQuanValue();
		String desc = userValue.getRemark();
		Integer optUserId = super.getLoginUser().getId();
		if (userId != null) {
			ret = hiRuleResultService.handleManualSave(userId, addDeleFlag, honorValue, quanValue, desc, optUserId);
		} else {
			ret.addErr("用户id为空！");
		}

		return ret;
	}

}
